package com.zc.im.service.im.group.impl;


import com.zc.im.mapper.im.group.ZcImGroupLogMapper;
import com.zc.im.model.im.group.*;
import com.zc.im.model.user.ZcUser;
import com.zc.im.service.im.group.ImGroupCommentService;
import com.zc.im.model.im.group.ZcImGroupLogModel;
import com.zc.im.model.im.group.ZcImGroup;
import com.zc.im.model.im.group.ZcImGroupLog;
import com.zc.im.model.im.group.ZcImGroupLogExample;
import com.zc.im.model.im.group.ZcImGroupLogKey;
import com.zc.im.service.im.group.ImGroupLogService;
import com.zc.im.service.im.group.ImGroupMarkScoreService;
import com.zc.im.service.im.group.ImGroupService;
import com.zc.im.service.user.UserService;
import com.zc.im.util.DateUtils;
import com.zc.im.util.ImGolbalTypeContext;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 日志
 * Created by wangcc on 2017/7/8.
 */
@Service
public class ImGroupLogServiceImpl implements ImGroupLogService {

    @Autowired
    private ZcImGroupLogMapper groupLogMapper;
    @Autowired
    private ImGroupService imGroupService;
    @Autowired
    private ImGroupCommentService imGroupCommentService;
    @Autowired
    private UserService userService;
    @Autowired
    private ImGroupMarkScoreService imGroupMarkScoreService;
    private static Logger logger = Logger.getLogger(ImGroupLogServiceImpl.class);


    /**
     * 添加日志
     * @param zcImGroupLogModel
     * @return
     */
    @Override
    public Integer insertGroupLog(ZcImGroupLog zcImGroupLogModel) {
        Integer groupLogId = null;
        if (zcImGroupLogModel == null) {
            logger.info("insert group log is null");
            return groupLogId;
        }
        zcImGroupLogModel.setCreatedTime(new Date());
        zcImGroupLogModel.setStatus(ImGolbalTypeContext.STATUS_VALID);
        try {
            groupLogId = groupLogMapper.insertSelective(zcImGroupLogModel);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return groupLogId;

    }


    /**
     * 删除日志
     * @param id
     * @return
     */
    @Override
    public Integer deleteGroupLog(Integer id) {
        Integer groupLogId = null;
        if (id == null) {
            logger.info("delete group log is null");
            return groupLogId;
        }
        try {
            ZcImGroupLogKey groupLogKey = new ZcImGroupLogKey();
            groupLogKey.setId(id);
            groupLogId = groupLogMapper.deleteByPrimaryKey(groupLogKey);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return groupLogId;

    }

    /**
     * 更新日志
     * @param zcImGroupLogModel
     * @return
     */
    @Override
    public Integer updateGroupLog(ZcImGroupLog zcImGroupLogModel) {
        Integer groupLogId = null;
        if (zcImGroupLogModel == null) {
            logger.info("update group log is null");
            return groupLogId;
        }
        zcImGroupLogModel.setUpdatedTime(new Date());
        try {
            ZcImGroupLogExample logExample = new ZcImGroupLogExample();
            ZcImGroupLogExample.Criteria criteria = logExample.createCriteria();
            criteria.andIdEqualTo(zcImGroupLogModel.getId());

            groupLogId = groupLogMapper.updateByExample(zcImGroupLogModel, logExample);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return groupLogId;

    }

    /**
     * 通过id 查询日志
     * @param groupLog
     * @return
     */
    @Override
    public List<ZcImGroupLog> queryIdGroupLog(ZcImGroupLog groupLog) {
        List<ZcImGroupLog> zcImGroupLogModels = null;
        if (groupLog == null) {
            logger.info("query group log is null    ");
            return zcImGroupLogModels;
        }
        try {
            ZcImGroupLogExample example = new ZcImGroupLogExample();
            ZcImGroupLogExample.Criteria criteria=example.createCriteria();
            criteria.andIdEqualTo(groupLog.getId());
            zcImGroupLogModels = groupLogMapper.selectByExample(example);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return zcImGroupLogModels;
    }

    /**
     *根据群groupId查询所有日志
     * @param groupLog
     * @return
     */
    public List<ZcImGroupLog> queryGroupIdGroupLog(ZcImGroupLog groupLog) {
        List<ZcImGroupLog> zcImGroupLogModels = null;
        if (groupLog == null) {
            logger.info("query groupLog log is null    ");
            return zcImGroupLogModels;
        }
        try {
            ZcImGroupLogExample example = new ZcImGroupLogExample();
            ZcImGroupLogExample.Criteria criteria = example.createCriteria();
            criteria.andGroupIdEqualTo(groupLog.getGroupId());
            zcImGroupLogModels = groupLogMapper.selectByExample(example);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return zcImGroupLogModels;
    }

    /**
     * 查询时间段的日志
     * @param groupLog
     * @return
     */
    public List<ZcImGroupLog> queryTimeGroupIdGroupLog(ZcImGroupLog groupLog) {
        List<ZcImGroupLog> zcImGroupLogModels = null;
        if (groupLog == null) {
            logger.info("query groupLog log is null    ");
            return zcImGroupLogModels;
        }
        try {
            ZcImGroupLogExample example = new ZcImGroupLogExample();
            ZcImGroupLogExample.Criteria criteria = example.createCriteria();
            criteria.andGroupIdEqualTo(groupLog.getGroupId());
            criteria.andCreatedTimeGreaterThan(groupLog.getCreatedTime());
            criteria.andCreatedTimeLessThan(groupLog.getUpdatedTime());
            zcImGroupLogModels = groupLogMapper.selectByExample(example);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return zcImGroupLogModels;
    }



    /**
     * 添加日志
     * @param zcImGroupLog
     * @return
     */
    @Override
    public Integer saveGroupLog(ZcImGroupLog zcImGroupLog) {
        Integer saveGroupLogKey = null;
        if (zcImGroupLog==null) {
            return saveGroupLogKey;
        }
        try {
            saveGroupLogKey = insertGroupLog(zcImGroupLog);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return saveGroupLogKey;
    }


    /**
     * 判断用户是不是群的管理员
     * @param Id
     * @param adminUserId
     * @return
     */
    @Override
    public List<ZcImGroupLog> queryGroupLog(Integer Id,Integer adminUserId) {
        List<ZcImGroupLog> listZcImGroupLog = null;
        if (Id==null || adminUserId==null) {
            return listZcImGroupLog;
        }
        ZcImGroup zcImGroup = new ZcImGroup();
        zcImGroup.setId(Id);
        try {
         ZcImGroup listZcImGroup = imGroupService.queryAllGroup(zcImGroup);
            if (listZcImGroup.getAdminUserId().equals(adminUserId)) {
                listZcImGroupLog=listGroupLog(Id);
            }else {
                logger.error("你不是群的管理员不能查看所有日志");
                return listZcImGroupLog;
            }
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }


        return listZcImGroupLog;
    }
    /**
     * 查找群所有的日志
     * @param Id
     * @return
     */
    public List<ZcImGroupLog>  listGroupLog(Integer Id) {
        List<ZcImGroupLog>  listZcImGroupLog = null;
        if (Id==null) {
            return listZcImGroupLog;
        }
        ZcImGroupLog zcImGroupLog = new ZcImGroupLog();
        zcImGroupLog.setGroupId(Id);
         try {
             listZcImGroupLog = queryGroupIdGroupLog(zcImGroupLog);
         }catch (Exception e) {
             logger.error(e.getMessage(),e);
         }
        return  listZcImGroupLog;
    }


    /**
     * 根据日志
     * @param logId
     * @return
     */
    @Override
    public List<ZcImGroupLogModel> queyGroupLogSocreMark(Integer logId) {
        List<ZcImGroupLogModel> zcImGroupLogModels = new ArrayList<>();
        ZcImGroupLogModel ZcImGroupLogModel=new ZcImGroupLogModel();
        if (logId==null) {
            return zcImGroupLogModels;
        }
        ZcImGroupComment zcImGroupComment = new ZcImGroupComment();
        zcImGroupComment.setLogId(logId);
        try {
             List<ZcImGroupComment> listZcImGroupComment = imGroupCommentService.queryGroupComment(zcImGroupComment);
             if (listZcImGroupComment==null || listZcImGroupComment.size()==0) {
                return zcImGroupLogModels;
             } else {
                 for(int i = 0; i<listZcImGroupComment.size();i++) {
                     ZcImGroupComment zcImGroupComment1 = listZcImGroupComment.get(i);
                     ZcUser zcUser = findGoupCommentUserId(listZcImGroupComment.get(i));
                     ZcImGroupMarkScore zcImGroupMarkScore = finZcImGroupMarkScore(listZcImGroupComment.get(i));
                     //添加评论日志时间
                     ZcImGroupLogModel.setCreatedTime(zcImGroupComment1.getCreatedTime());
                     //添加日志内容
                     ZcImGroupLogModel.setContent(zcImGroupComment1.getContent());
                     //添加评论人头像
                     ZcImGroupLogModel.setPicHead(zcUser.getPicHead());
                     //添加评论人昵称
                     ZcImGroupLogModel.setNickname(zcUser.getNickname());
                     //加入日志分数
                     ZcImGroupLogModel.setScore(zcImGroupMarkScore.getScore());
                     //加入状态
                     zcImGroupComment1.setStatus(ImGolbalTypeContext.STATUS_VALID);
                     zcImGroupLogModels.add(ZcImGroupLogModel);
                      }
                    }
          }catch (Exception e) {
               logger.error(e.getMessage(),e);
        }
        return zcImGroupLogModels;

}



    /**
     * 查询评论人的头像与昵称
     * @param zcImGroupComment
     * @return
     */
    private ZcUser findGoupCommentUserId(ZcImGroupComment zcImGroupComment) {
         ZcUser zcUser = null;
         if (zcImGroupComment==null) {
                return zcUser;
         }
         try {
             zcUser = userService.queryUser(zcImGroupComment.getCommentUserId());
         }catch (Exception e) {
             logger.error(e.getMessage(),e);
         }
            return zcUser;
        }

    /**
     * 根据日志id查询改日志的评分
     * @param zcImGroupComment
     * @return
     */
    public ZcImGroupMarkScore  finZcImGroupMarkScore(ZcImGroupComment zcImGroupComment) {
        ZcImGroupMarkScore zcImGroupMarkScore=null;
        if (zcImGroupComment==null) {
            return  zcImGroupMarkScore;
        }
        try {
            zcImGroupMarkScore = imGroupMarkScoreService.queryGroupMarkScore(zcImGroupComment.getLogId());
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return zcImGroupMarkScore;
        }

    /**
     * 通多时间检索出日志
     * @param groupId
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public List<ZcImGroupLog> queryTimeGroupId(Integer id,Integer groupId, String startTime, String endTime) {
        List<ZcImGroupLog> zcImGroupLogs = null;
        if (id==null || groupId==null || startTime==null ||endTime==null) {
            return zcImGroupLogs;
        }
        ZcImGroup zcImGroup  = new ZcImGroup();
        zcImGroup.setId(groupId);
        try {
            ZcImGroup zcImGroup1 = imGroupService.queryAllGroup(zcImGroup);
            if (zcImGroup1.getAdminUserId().equals(id)) {
                zcImGroupLogs=queryStartEndTimeGroupLog(groupId,startTime,endTime);
            } else {
                logger.error("只有管理员才能查看日志");
            }
        }catch (Exception e) {
        }
        return zcImGroupLogs;
    }
    /**
     * 查询时间段的日志
     * @param groupId
     * @param startTime
     * @param endTime
     * @return
     */
    private List<ZcImGroupLog> queryStartEndTimeGroupLog(Integer groupId, String startTime, String endTime) {
        List<ZcImGroupLog> zcImGroupLogs = null;
        if ( groupId==null || startTime==null ||endTime==null) {
            return zcImGroupLogs;
        }
        ZcImGroupLog zcImGroupLog = new ZcImGroupLog();
        zcImGroupLog.setGroupId(groupId);
        zcImGroupLog.setCreatedTime(DateUtils.strFormatDateYmd(startTime));
        zcImGroupLog.setUpdatedTime(DateUtils.strFormatDateYmd(endTime));
        try {
            zcImGroupLogs = queryTimeGroupIdGroupLog(zcImGroupLog);
            if (zcImGroupLog==null) {
                logger.error("查询时间段时间为空，无记录");
                return zcImGroupLogs;
            }
        } catch ( Exception e) {
            logger.error(e.getMessage(),e);
        }
        return zcImGroupLogs;
    }


}

